var wMaster = function(){
	
	this.tabWin = function(pActive){
		return gWin(pActive);
	}
	
	var gWin = function(pActive){
	
		var tabWin = Titanium.UI.createWindow({
			fullscreen: false,
		});
		
		tabWin.add(gTop(pActive));
		tabWin.add(gContent(pActive));
		tabWin.add(gBottom(pActive));
		
		return tabWin;
	}

	var gTop = function(pActive){
		var top = Titanium.UI.createView({
			top: 0,
			height: 44,
			backgroundImage: "media/images/bar-top.png",
		});
		var title = Ti.UI.createImageView({
			left: 0,
			top: 0,
			width: 320,
			height: 44,
			zIndex: 99
		})
		top.add(title);
		if(pActive == "news"){
			title.image = 'media/home/images/magic.png';
		}
		if(pActive == "images"){
			title.image = 'media/home/images/coverflow.png';
		}
		if(pActive == "video"){
			title.image = 'media/home/images/video.png';
		}
		if(pActive == "weibo"){
			title.image = 'media/home/images/weibo.png';
		}
		if(pActive == "more"){
			title.image = 'media/home/images/more.png';
		}
		
		
		return top;
	}
	
	var gContent = function(pActive){
		var content = Titanium.UI.createView({
			top: 44,
		});

		Ti.include('content.js');
		var wCon = new wContent();
		if(pActive == "news"){
			content.add(wCon.news());
			content.backgroundImage = "media/images/bghome.png";
		}
		if(pActive == "images"){
			content.add(wCon.images());	
		}
		if(pActive == "video"){
			content.add(wCon.video());
			//Ti.Accelerometer.addEventListener('update', function(e){
			//   Ti.API.debug("accelerometer - x:"+e.x+",y:"+e.y+",z:"+e.z);
			//});
			
		}
		if(pActive == "weibo"){
			content.add(wCon.weibo());	
		}
		if(pActive == "more"){
			content.add(wCon.more());	
		}
		
	
		return content;
	}
	
	var gBottom = function(pActive){
		
		Ti.include('dump.js');
		var versionFile = Titanium.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, 'data/version.js');
		if(versionFile.exists()){
			var v = versionFile.read().text;
		}
		var uD = new uDumper();
		
		var bottom = Titanium.UI.createView({
			top: 411,
			height: 49,
			backgroundImage: "media/images/bar-bottom.png",
		});
		
		var btnNews = Titanium.UI.createButton({
			left: 0,
			width: 64,
			backgroundImage: "media/nav/bar1_1.png"
		});
		bottom.add(btnNews);
		btnNews.addEventListener('click', function(e){
			uD.goUpdate(v);
			btnNews.backgroundImage = "media/nav/bar1_2.png";
			btnImages.backgroundImage = "media/nav/bar2_1.png";
			btnVideo.backgroundImage = "media/nav/bar3_1.png";
			btnWeibo.backgroundImage = "media/nav/bar4_1.png";
			btnMore.backgroundImage = "media/nav/bar5_1.png";
			
			//Ti.UI.currentWindow.close();
			gWin("news").open({
				transition: Ti.UI.ANIMATION_CURVE_EASE_IN 
			});
		});
		
		var btnImages = Titanium.UI.createButton({
			left: 64,
			width: 64,
			backgroundImage: "media/nav/bar2_1.png",
		});
		bottom.add(btnImages);
		btnImages.addEventListener('click', function(e){
			uD.goUpdate(v);
			btnNews.backgroundImage = "media/nav/bar1_1.png";
			btnImages.backgroundImage = "media/nav/bar2_2.png";
			btnVideo.backgroundImage = "media/nav/bar3_1.png";
			btnWeibo.backgroundImage = "media/nav/bar4_1.png";
			btnMore.backgroundImage = "media/nav/bar5_1.png";
			
			//Ti.UI.currentWindow.close();
			gWin("images").open({
				transition: Ti.UI.ANIMATION_CURVE_EASE_IN
			});
		});
		
		var btnVideo = Titanium.UI.createButton({
			left: 128,
			width: 64,
			backgroundImage: "media/nav/bar3_1.png"
		});
		bottom.add(btnVideo);
		btnVideo.addEventListener('click', function(e){
			uD.goUpdate(v);
			btnNews.backgroundImage = "media/nav/bar1_1.png";
			btnImages.backgroundImage = "media/nav/bar2_1.png";
			btnVideo.backgroundImage = "media/nav/bar3_2.png";
			btnWeibo.backgroundImage = "media/nav/bar4_1.png";
			btnMore.backgroundImage = "media/nav/bar5_1.png";
			
			//Ti.UI.currentWindow.close();
			gWin("video").open({
				transition: Ti.UI.ANIMATION_CURVE_EASE_IN
			});
		});
		
		var btnWeibo = Titanium.UI.createButton({
			left: 192,
			width: 64,
			backgroundImage: "media/nav/bar4_1.png"
		});
		bottom.add(btnWeibo);
		btnWeibo.addEventListener('click', function(e){
			btnNews.backgroundImage = "media/nav/bar1_1.png";
			btnImages.backgroundImage = "media/nav/bar2_1.png";
			btnVideo.backgroundImage = "media/nav/bar3_1.png";
			btnWeibo.backgroundImage = "media/nav/bar4_2.png";
			btnMore.backgroundImage = "media/nav/bar5_1.png";
			
			//Ti.UI.currentWindow.close();
			gWin("weibo").open({
				transition: Ti.UI.ANIMATION_CURVE_EASE_IN
			});
		});
		
		var btnMore = Titanium.UI.createButton({
			left: 256,
			width: 64,
			backgroundImage: "media/nav/bar5_1.png"
		});
		bottom.add(btnMore);
		btnMore.addEventListener('click', function(e){
			uD.goUpdate(v);
			btnNews.backgroundImage = "media/nav/bar1_1.png";
			btnImages.backgroundImage = "media/nav/bar2_1.png";
			btnVideo.backgroundImage = "media/nav/bar3_1.png";
			btnWeibo.backgroundImage = "media/nav/bar4_1.png";
			btnMore.backgroundImage = "media/nav/bar5_2.png";

			//Ti.UI.currentWindow.close();
			gWin("more").open({
				transition: Ti.UI.ANIMATION_CURVE_EASE_IN
			});
		});
		
		if(pActive == "news"){
			btnNews.backgroundImage = "media/nav/bar1_2.png";
		}
		if(pActive == "images"){
			btnImages.backgroundImage = "media/nav/bar2_2.png";
		}
		if(pActive == "video"){
			btnVideo.backgroundImage = "media/nav/bar3_2.png";
		}
		if(pActive == "weibo"){
			btnWeibo.backgroundImage = "media/nav/bar4_2.png";
		}
		if(pActive == "more"){
			btnMore.backgroundImage = "media/nav/bar5_2.png";
		}
		
		return	bottom;
	}
	
	
}